<?php
// +----------------------------------------------------------------------
// | EtrolPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Author: Author: HCZ <huangchengze@etrol.com>
// +----------------------------------------------------------------------

namespace app\admin\controller;

use app\common\controller\Admin;
use app\admin\controller\api\Workflow as WorkflowApi;
use app\admin\controller\api\Workstep as WorkstepApi;
use app\admin\controller\ajax\Library as LibraryAjax;
/**
 * 流程管理
 * @package app\admin
 */
class Workflow  extends Admin

{
	protected $api;
	protected $property;

	public function _initialize(){
		parent::_initialize();
		$this->api = new WorkflowApi();
		$this->stepApi = new WorkstepApi();
		$this->property = new  LibraryAjax();
	}

	/**
	 * 流程列表
	 * @return View/Json
	 */
	public function index(){
		if(IS_AJAX){
			return $this->api->workflowList();
		}
		return $this->fetch();
	}
	
	/**
	 * 流程添加
	 * @return View/Json
	 */
	public function add(){
		if(IS_POST){
			$data = input('post.');
			$data['operationId'] = session('user_auth.id');
			return $this->api->add($data);
		}
		$workflowType = $this->property->getWorkflowType()->getData();
		$this->assign('workflowType',$workflowType);
		return $this->fetch();
	}
	
	/**
	 * 流程编辑
	 * @param string $workflowId
	 * @return View/Json
	 */
	public function edit($workflowId){
		if(IS_POST){
			$data = input('post.');
			$data['operationId'] = session('user_auth.id');
			return $this->api->edit($data,$workflowId);
		}
		$workflowType = $this->property->getWorkflowType()->getData();
		$this->assign('workflowType',$workflowType);
		$workflow = model('Workflow')->find($workflowId);
		$this->assign('info',$workflow);
		return $this->fetch();
	}
	
	/**
	 * 流程删除
	 * @return Json
	 */
	public function del(){
		if(IS_GET){
			return $this->api->del(input('workflowId'));
		}
	}
	
	public function search(){
		if(IS_AJAX){
			return $this->api->search(input('keyword'));
		}
	}
	
	/**
	 * 详细步骤
	 * @param string $workflowId
	 * @return View
	 */
	public function detail($workflowId){
		if(IS_AJAX){
			return $this->stepApi->workstepList($workflowId);
		}
		return $this->error('页面错误');
	}
	
	/**
	 * 新增步骤
	 * @param string $workflowId
	 * @return Json/View
	 */
	public function addStep($workflowId = null){
		if(IS_POST){
			$data = input('post.');
			return $this->stepApi->add($data);
		}
		$userList = $this->stepApi->getUser()->getData();
		$this->assign('userList',$userList['data']);
		$this->assign('workflowId',$workflowId);
		return $this->fetch();
	}
	
	/**
	 * 编辑步骤
	 * @param string $stepId
	 * @return Json/View
	 */
	public function editStep($stepId = null){
		if(IS_POST){
			$data = input('post.');
			return $this->stepApi->edit($data,$stepId);
		}
		$step = model('Workstep')->find($stepId);
		$this->assign('info',$step);
		$userList = $this->stepApi->getUser()->getData();
		$this->assign('userList',$userList['data']);
		return $this->fetch();
	}
	
	/**
	 * 删除步骤
	 * @return Json
	 */
	public function delStep(){
		if(IS_GET){
			return $this->stepApi->del(input('stepId'));
		}
	}
	
	

}